#include "strap.h"

Strap::Strap()
{
    this->setFlag(QGraphicsItem::ItemIsMovable);
}

QRectF Strap::boundingRect() const
{
    return QRectF(0,0,strapHeight,strapWidth);
}

void Strap::printDiffCifFormat(QTextStream *out)
{
    QRectF diff(Rules::getRule("NP.E.1"),Rules::getRule("NP.E.1"),strapHeight -(Rules::getRule("NP.E.1")*2), (Rules::getRule("CO.E.1")*2) + Rules::getRule("CO.W.1"));

    QPointF bottomLeft = mapToScene(diff.bottomLeft());
    QPointF bottomRight = mapToScene(diff.bottomRight());
    QPointF topRight = mapToScene(diff.topRight());
    QPointF topLeft = mapToScene(diff.topLeft());

    QString p;

    p = QString::number(bottomLeft.x()*10) + "," + QString::number(bottomLeft.y()*10);
    p += " " + QString::number(bottomRight.x()*10) + "," + QString::number(bottomRight.y()*10);
    p += " " + QString::number(topRight.x()*10) + "," + QString::number(topRight.y()*10);
    p += " " + QString::number(topLeft.x()*10) + "," + QString::number(topLeft.y()*10) + ";";

    *out << "P " + p << endl;
}

void Strap::printCoCifFormat(QTextStream *out)
{
    QRectF contRect(6,6,9,9);

    while(true)
    {
        QPointF bottomLeft = mapToScene(contRect.bottomLeft());
        QPointF bottomRight = mapToScene(contRect.bottomRight());
        QPointF topRight = mapToScene(contRect.topRight());
        QPointF topLeft = mapToScene(contRect.topLeft());

        QString p;

        p = QString::number(bottomLeft.x()*10) + "," + QString::number(bottomLeft.y()*10);
        p += " " + QString::number(bottomRight.x()*10) + "," + QString::number(bottomRight.y()*10);
        p += " " + QString::number(topRight.x()*10) + "," + QString::number(topRight.y()*10);
        p += " " + QString::number(topLeft.x()*10) + "," + QString::number(topLeft.y()*10) + ";";

        *out << "P " + p << endl;

        contRect.translate(23,0);

        if(contRect.right() + 6 > this->strapHeight)
            break;
    }
}

void Strap::printNpPpCifFormat(QTextStream *out)
{
    QPointF bottomLeft = mapToScene(boundingRect().bottomLeft());
    QPointF bottomRight = mapToScene(boundingRect().bottomRight());
    QPointF topRight = mapToScene(boundingRect().topRight());
    QPointF topLeft = mapToScene(boundingRect().topLeft());

    QString p;

    p = QString::number(bottomLeft.x()*10) + "," + QString::number(bottomLeft.y()*10);
    p += " " + QString::number(bottomRight.x()*10) + "," + QString::number(bottomRight.y()*10);
    p += " " + QString::number(topRight.x()*10) + "," + QString::number(topRight.y()*10);
    p += " " + QString::number(topLeft.x()*10) + "," + QString::number(topLeft.y()*10) + ";";

    *out << "P " + p << endl;

}

void Strap::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QBrush brush;
    QPen pen;
    QTransform tr;
    tr.scale(1/(*scale),1/(*scale));
    brush.setTransform(tr);
    pen.setWidth(0);

    painter->setOpacity(0.5);
    brush.setStyle(Qt::Dense7Pattern);

    if(this->strapType.compare("NTAP",Qt::CaseInsensitive) == 0)
    {
        brush.setColor(Qt::yellow);
        pen.setColor(Qt::yellow);
    }
    else
    {
        brush.setColor(Qt::white);
        pen.setColor(Qt::white);
    }

    this->setZValue(-1);
    painter->setBrush(brush);
    painter->setPen(pen);
    painter->drawRect(boundingRect());

    painter->setOpacity(1);

    //DIFF
    brush.setColor(QColor(0,0XCC,0x66));
    brush.setStyle(Qt::Dense6Pattern);
    pen.setColor(QColor(0,0XCC,0x66));
    painter->setBrush(brush);
    painter->setPen(pen);


    QRectF diff(Rules::getRule("NP.E.1"),
                Rules::getRule("NP.E.1"),
                strapHeight - (Rules::getRule("NP.E.1")*2),
                (Rules::getRule("CO.E.1")*2) + Rules::getRule("CO.W.1"));

    painter->drawRect(diff);

    //CONT
    brush.setColor(Qt::transparent);
    brush.setStyle(Qt::SolidPattern);
    pen.setColor(QColor(0XFF,0xFF,0));
    painter->setBrush(brush);
    painter->setPen(pen);


    QRectF contRect(Rules::getRule("NP.E.1") + Rules::getRule("CO.E.1"),
                    Rules::getRule("NP.E.1") + Rules::getRule("CO.E.1"),
                    Rules::getRule("CO.W.1"),
                    Rules::getRule("CO.W.1"));

    while(true)
    {
        painter->drawRect(contRect);
        painter->drawLine(contRect.topLeft(),contRect.bottomRight());
        painter->drawLine(contRect.bottomLeft(),contRect.topRight());

        contRect.translate(Rules::getRule("CO.W.1") + Rules::getRule("CO.S.1"),0);

        if(contRect.right() + Rules::getRule("CO.E.1") + Rules::getRule("NP.E.1") > this->strapHeight)
            break;
    }
}




